package cc.mrbird.febs.buss.controller;

import cc.mrbird.febs.common.annotation.ControllerEndpoint;
import cc.mrbird.febs.common.utils.FebsUtil;
import cc.mrbird.febs.common.entity.FebsConstant;
import cc.mrbird.febs.common.controller.BaseController;
import cc.mrbird.febs.common.entity.FebsResponse;
import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.buss.entity.MenuItemText;
import cc.mrbird.febs.buss.service.IMenuItemTextService;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.HashMap;
import java.util.Enumeration;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Map;

/**
 *  Controller
 *
 * @author Lujingyuan
 * @date 2021-10-08 17:10:44
 */
@Slf4j
@Validated
@Controller
public class MenuItemTextController extends BaseController {

    @Autowired
    private IMenuItemTextService menuItemTextService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "menuItemText")
    public String menuItemTextIndex(){
        return FebsUtil.view("menuItemText/menuItemText");
    }

    @GetMapping("menuItemText")
    @ResponseBody
    @RequiresPermissions("menuItemText:list")
    public FebsResponse getAllMenuItemTexts(MenuItemText menuItemText) {
        return new FebsResponse().success().data(menuItemTextService.findMenuItemTexts(menuItemText));
    }

    @GetMapping("menuItemText/all")
    @ResponseBody
    public FebsResponse getAll(MenuItemText f,HttpServletRequest req) {
        Enumeration enu=req.getParameterNames();
        Map<String,Object> map=new HashMap<String,Object>();
        while(enu.hasMoreElements()){
            String paraName=(String)enu.nextElement();
            map.put(paraName,req.getParameter(paraName));
        }
        if(map.get("keyword")!=null&& StringUtils.isNotBlank(map.get("keyword").toString().trim())){
            map.put("name",map.get("keyword"));
        }
        List<MenuItemText> list=menuItemTextService.selectByMap(map);
        return new FebsResponse().success().data(list);
    }

    @GetMapping("menuItemText/list")
    @ResponseBody
    public FebsResponse menuItemTextList(QueryRequest request, HttpServletRequest req, MenuItemText menuItemText) {
        Enumeration enu=req.getParameterNames();
        HashMap<String,String> map=new HashMap<String,String>();
        while(enu.hasMoreElements()){
            String paraName=(String)enu.nextElement();
            map.put(paraName,req.getParameter(paraName));
        }
        Map<String, Object> dataTable = getDataTable(this.menuItemTextService.findMenuItemTextDetail(request, map));
        return new FebsResponse().success().data(dataTable);
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "menuItemText/add")
    public String goMenuItemTextAdd() {
        return FebsUtil.view("menuItemText/menuItemTextAdd");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "menuItemText/update/{id}")
    public String goMenuItemTextUpdate(@PathVariable String id, Model model) {
        MenuItemText obj= menuItemTextService.getById(id);
        model.addAttribute("obj",obj);
        return FebsUtil.view("menuItemText/menuItemTextUpdate");
    }

    @ControllerEndpoint(operation = "新增MenuItemText", exceptionMessage = "新增MenuItemText失败")
    @PostMapping("menuItemText")
    @ResponseBody
    public FebsResponse addMenuItemText(@Valid MenuItemText menuItemText) {
        this.menuItemTextService.add(menuItemText);
        return new FebsResponse().success();
    }

    @GetMapping("menuItemText/delete/{objIds}")
    @ControllerEndpoint(operation = "删除", exceptionMessage = "删除失败")
    @ResponseBody
    public FebsResponse deleteMenuItemText( @PathVariable String objIds) {
        String[] ids = objIds.split(StringPool.COMMA);
        this.menuItemTextService.deleteMenuItemText(ids);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "修改MenuItemText", exceptionMessage = "修改MenuItemText失败")
    @PostMapping("menuItemText/update")
    @ResponseBody
    public FebsResponse updateMenuItemText(MenuItemText menuItemText) {
        this.menuItemTextService.updateMenuItemText(menuItemText);
        return new FebsResponse().success();
    }
}
